const ClockModel = require("../model/clockmodel")
const Resultmodel = require("../model/resultmodel")
const {
	TypeEnum,
	CodeEnum,
} = require("../enum");
module.exports = class Utils {
	static suc(data, msg) {
		return Resultmodel.success(data, msg);
	}
	static err(msg, code) {
		
		if(Number.isInteger(msg)){
			code = msg 
			msg = ""
		}else{
			code = CodeEnum.ERROR
		}
		return Resultmodel.error(msg, code);
	}
	static exc( data, msg  ) {
		return Resultmodel.exception(data ,msg);
	}
	static getLastDayInMonth(year, month) {
		return ClockModel.getLastDayInMonth(year, month);
	}
	static getDayInWeek(inferredDay, dt) {
		return ClockModel.getDayInWeek(inferredDay, dt);
	}

	static sleep(d) {
		for (let t = Date.now(); Date.now() - t <= d;);
	}

	static now(pattern, nowDT) {
		return ClockModel.now(pattern, nowDT)
	}

	static clone(obj) {
		let buf;
		if (obj instanceof Array) {
			buf = [];
			let lg = obj.length;
			while (lg--) {
				buf[lg] = clone(obj[lg]);
			}
			return buf;
		} else if (obj instanceof Object) {
			buf = {};
			for (let key in obj) {
				buf[key] = clone(obj[key])
			}
		} else {
			return obj;
		}
	}


	// 判断类型
	static typeOf(sour, tarType) {
		const args = Array.from(arguments);
		const sourInstType = Object.prototype.toString.call(sour);
		const map = {
			'[object Boolean]': TypeEnum.BOOLEAN,
			'[object Number]': TypeEnum.NUMBER,
			'[object String]': TypeEnum.STRING,
			'[object Function]': TypeEnum.FUNCTION,
			'[object Array]': TypeEnum.ARRAY,
			'[object Date]': TypeEnum.DATE,
			'[object RegExp]': TypeEnum.REGEXP,
			'[object Undefined]': TypeEnum.UNDEFINED,
			'[object Null]': TypeEnum.NULL,
			'[object Object]': TypeEnum.OBJECT
		};
		const mapType = map[sourInstType];
		return args.length > 1 ? tarType.split(",").includes(mapType) : mapType
	}


	static isBoolean(preProp) {
		return Utils.typeOf(preProp, TypeEnum.BOOLEAN);
	}

	static isNumber(preProp) {
		return Utils.typeOf(preProp, TypeEnum.NUMBER);
	}

	static isString(preProp) {
		return Utils.typeOf(preProp, TypeEnum.STRING);
	}

	static isFunction(preProp) {
		return Utils.typeOf(preProp, TypeEnum.FUNCTION);
	}

	static isArray(preProp) {
		return Utils.typeOf(preProp, TypeEnum.ARRAY);
	}

	static isDate(preProp) {
		return Utils.typeOf(preProp, TypeEnum.DATE);
	}

	static isRegExp(preProp) {
		return Utils.typeOf(preProp, TypeEnum.REGEXP);
	}

	static isUndefined(preProp) {
		return Utils.typeOf(preProp, TypeEnum.UNDEFINED);
	}

	static isNull(preProp) {
		return Utils.typeOf(preProp, TypeEnum.NULL);
	}

	static isObject(preProp) {
		return Utils.typeOf(preProp, TypeEnum.OBJECT);
	}

	static isNoBoolean(preProp) {
		return !Utils.typeOf(preProp, TypeEnum.BOOLEAN);
	}

	static isNoNumber(preProp) {
		return !Utils.typeOf(preProp, TypeEnum.NUMBER);
	}

	static isNoString(preProp) {
		return !Utils.typeOf(preProp, TypeEnum.STRING);
	}

	static isNoFunction(preProp) {
		return !Utils.typeOf(preProp, TypeEnum.FUNCTION);
	}

	static isNoArray(preProp) {
		return !Utils.typeOf(preProp, TypeEnum.ARRAY);
	}

	static isNoDate(preProp) {
		return !Utils.typeOf(preProp, TypeEnum.DATE);
	}

	static isNoRegExp(preProp) {
		return !Utils.typeOf(preProp, TypeEnum.REGEXP);
	}

	static isNoUndefined(preProp) {
		return !Utils.typeOf(preProp, TypeEnum.UNDEFINED);
	}

	static isNoNull(preProp) {
		return !Utils.typeOf(preProp, TypeEnum.NULL);
	}

	static isNoObject(preProp) {
		return !Utils.typeOf(preProp, TypeEnum.OBJECT);
	}


}
